package com.lcf.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.base.exception.SysException;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dao.SysDictTypeDao;
import com.lcf.system.dto.dict.SysDictTypeDTO;
import com.lcf.framework.model.system.SysDict;
import com.lcf.framework.model.system.SysDictType;
import com.lcf.system.manager.SysDictTypeManager;
import com.lcf.system.query.dict.SysDictTypePageQuery;
import com.lcf.system.query.dict.SysDictTypeQuery;
import com.lcf.system.service.SysDictTypeService;
import com.lcf.system.vo.dict.SysDictTypeDetailVO;
import com.lcf.system.vo.dict.SysDictTypeVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典类型(SysDictType)表服务实现类
 *
 * @author lcf
 * @since 2025-03-16 22:04:24
 */
@Service("sysDictTypeService")
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeDao, SysDictType> implements SysDictTypeService {
    @Resource
    private SysDictTypeManager sysDictTypeManager;

    @Override
    public void add(SysDictTypeDTO dto) {
        sysDictTypeManager.checkExist(null, dto);

        BeanUtil.copyProperties(dto, SysDictType.class).insert();
    }

    @Override
    public void edit(long dictTypeId, SysDictTypeDTO dto) {
        sysDictTypeManager.checkExist(dictTypeId, dto);

        SysDictType sysDictType = BeanUtil.copyProperties(dto, SysDictType.class);
        sysDictType.setId(dictTypeId);
        sysDictType.updateById();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(long dictTypeId) {
        sysDictTypeManager.checkId(dictTypeId);
        //检查是否已被使用
        boolean exists = Db.lambdaQuery(SysDict.class)
                .eq(SysDict::getTypeId, dictTypeId)
                .exists();

        if (exists) {
            throw new SysException("字典类型已被使用");
        }

        removeById(dictTypeId);

        //删除字典
        Db.lambdaUpdate(SysDict.class)
                .eq(SysDict::getTypeId, dictTypeId)
                .remove();
    }

    @Override
    public SysDictTypeDetailVO get(long dictTypeId) {
        SysDictType sysDictType = getById(dictTypeId);

        return BeanUtil.copyProperties(sysDictType, SysDictTypeDetailVO.class);
    }

    @Override
    public List<SysDictTypeVO> list(SysDictTypeQuery query) {
        List<SysDictType> list = lambdaQuery()
                .eq(StringUtils.isNotBlank(query.getTypeCode()), SysDictType::getTypeCode, query.getTypeCode())
                .like(StringUtils.isNotBlank(query.getTypeName()), SysDictType::getTypeName, query.getTypeName())
                .list();

        return BeanUtil.copyToList(list, SysDictTypeVO.class);
    }

    @Override
    public PageVO<SysDictTypeVO> page(SysDictTypePageQuery query) {
        Page<SysDictType> page = lambdaQuery()
                .eq(StringUtils.isNotBlank(query.getTypeCode()), SysDictType::getTypeCode, query.getTypeCode())
                .like(StringUtils.isNotBlank(query.getTypeName()), SysDictType::getTypeName, query.getTypeName())
                .page(query.toPage());

        return new PageVO<>(page, SysDictTypeVO.class);
    }
}

